package com.quanyan.stadiumScheduler.service.impl;

import com.quanyan.common.utils.DateUtils;
import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.stadiumScheduler.biz.BizGameSystemDao;
import com.quanyan.stadiumScheduler.cache.MyRedisTemplate;
import com.quanyan.stadiumScheduler.cache.StadiumRedisConstants;
import com.quanyan.stadiumScheduler.constants.GameConstants;
import com.quanyan.stadiumScheduler.entity.*;
import com.quanyan.stadiumScheduler.entity.vo.StadiumNotifyMsg;
import com.quanyan.stadiumScheduler.entity.vo.VGameEventCollect;
import com.quanyan.stadiumScheduler.mapper.TbGameCollectMapper;
import com.quanyan.stadiumScheduler.mapper.TbGameEventMapper;
import com.quanyan.stadiumScheduler.mapper.VStadiumUserDefinedMapper;
import com.quanyan.stadiumScheduler.service.BaseService;
import com.quanyan.stadiumScheduler.service.DataReductionService;
import com.quanyan.stadiumScheduler.service.GameMsgPush;
import com.quanyan.statium.service.StadiumInternalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author lds
 * @date 2017/2/14 15:10
 */

@Service
public class DataReductionServiceImpl implements DataReductionService {

    private final static Logger logger = LoggerFactory.getLogger(DataReductionServiceImpl.class);

    @Autowired
    private BizGameSystemDao bizGameSystemDao;

    @Autowired
    private VStadiumUserDefinedMapper vStadiumUserDefinedMapper;

    @Autowired
    private TbGameEventMapper tbGameEventMapper;

    @Autowired
    private BaseService baseService;

    @Autowired
    private TbGameCollectMapper tbGameCollectMapper;

    @Autowired
    private MyRedisTemplate myRedisTemplate;

    @Autowired
    private GameMsgPush gameMsgPush;

    @Autowired
    private StadiumInternalService stadiumInternalService;

    //一小时推荐
    private final static int ONE_HOUR_MIN = 60;

    @Override
    public void gameBeforeOneHourEventStartRemind(int min) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andStartTimeBetween(DateUtils.addTime(new Date(), (ONE_HOUR_MIN - min), DateUtils.TIME_TYPE_MINUTE), DateUtils.addTime(new Date(), ONE_HOUR_MIN, DateUtils.TIME_TYPE_MINUTE));
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        List<TbGameEvent> tbGameEventList = tbGameEventMapper.selectByExample(example);
        if (null!=tbGameEventList && tbGameEventList.size() >0) {
            for (TbGameEvent tbGameEvent:tbGameEventList) {
                RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, StadiumRedisConstants.getGameEventStartKey(tbGameEvent.getId()));
                boolean flag = myRedisTemplate.setnxWithTime(redisKey, ONE_HOUR_MIN * 1000);
                if (!flag) {
                    gameMsgPush.pushBeforeOneHourGameStartMsgToUser(tbGameEvent, this.queryUidsFromTbGameEvent(tbGameEvent));
                }
            }
        }
    }

    @Override
    public void updateGameEventMsg() {
        logger.info("开始实时刷新官方事和pk赛事的数据统计");
        // 官方赛事更新,每次更新将以前的数据逻辑删除
        TbGameCollectExample example = new TbGameCollectExample();
        TbGameCollect temp = new TbGameCollect();
        temp.setIsDelete(GameConstants.IS_DELETE);
        tbGameCollectMapper.updateByExampleSelective(temp, example);

        //更新官方赛事
        List<VGameEventCollect> resultTemp = vStadiumUserDefinedMapper.selectVGameEventCollect();
        if (null!=resultTemp && resultTemp.size()>0) {
            for (VGameEventCollect collect : resultTemp) {
                TbGameCollect tbGameCollect = new TbGameCollect();
                tbGameCollect.setGameCategoryId(collect.getCategoryId());
                tbGameCollect.setCityId(collect.getDistrictId());
                tbGameCollect.setDistrictId(collect.getDistrictId());
                tbGameCollect.setGameEventNum(collect.getGameEventNum());
                tbGameCollect.setGametype(GameConstants.GAME_TYPE_OFFICIAL);
                tbGameCollectMapper.insertSelective(tbGameCollect);
            }
        }

        //更新pk赛事
        List<VGameEventCollect> pkResultTemp = vStadiumUserDefinedMapper.selectVpkGameEventCollect();
        if (null!=pkResultTemp && pkResultTemp.size()>0) {
            for (VGameEventCollect collect : pkResultTemp) {
                TbGameCollect tbGameCollect = new TbGameCollect();
                tbGameCollect.setGameCategoryId(collect.getCategoryId());
                tbGameCollect.setGameEventNum(collect.getGameEventNum());
                tbGameCollect.setGametype(GameConstants.GAME_TYPE_PK);
                tbGameCollectMapper.insertSelective(tbGameCollect);
            }
        }
        logger.info("结束实时刷新官方事和pk赛事的数据统计");
    }

    @Override
    public List<Integer> queryUidsFromTbGameEvent(TbGameEvent tbGameEvent) {
        List<TbGameEventTeam> teams = bizGameSystemDao.selectTbGameEventTeamByGameEventId(tbGameEvent.getId(), GameConstants.IS_NOT_DELETE);
        List<Integer> uids = new ArrayList<>();
        List<Integer> clubIds = new ArrayList<>();
        for(TbGameEventTeam tbGameEventTeam :teams) {
            if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
                clubIds.add(tbGameEventTeam.getClubId());
            }else{
                uids.add(tbGameEventTeam.getUid());
            }
        }
        if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
            Map<Integer, List<Integer>> map = baseService.queryClubIds(clubIds);
            Set<Integer> tempIds = new HashSet<>();
            for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
                if (null!=entry.getValue() && entry.getValue().size()>0) {
                    tempIds.addAll(entry.getValue());
                }
            }
            uids = new ArrayList<>(tempIds);
        }
        return uids;
    }


    @Override
    public void userGameScheduleInit(StadiumNotifyMsg stadiumNotifyMsg) {
        stadiumInternalService.userGameScheduleInit(stadiumNotifyMsg);
    }

    @Override
    public void userGameScheduleUpdate(StadiumNotifyMsg stadiumNotifyMsg) {
        stadiumInternalService.userGameScheduleUpdate(stadiumNotifyMsg);
    }

    @Override
    public void userGameScheduleFinally(StadiumNotifyMsg stadiumNotifyMsg) {
        stadiumInternalService.userGameScheduleFinally(stadiumNotifyMsg);
    }

    @Override
    public void userGameEventCollect(StadiumNotifyMsg stadiumNotifyMsg) {
        stadiumInternalService.userGameEventCollect(stadiumNotifyMsg);
    }

    @Override
    public void generateCurrentRoundPushMsgAndMQ(StadiumNotifyMsg stadiumNotifyMsg) {
        stadiumInternalService.generateCurrentRoundPushMsgAndMQ(stadiumNotifyMsg);
    }

    @Override
    public void officialGameEventPushMsg(StadiumNotifyMsg stadiumNotifyMsg) {
        stadiumInternalService.officialGameEventPushMsg(stadiumNotifyMsg);
    }

    @Override
    public void officialGameEventCollectMsg(StadiumNotifyMsg stadiumNotifyMsg) {
        stadiumInternalService.officialGameEventCollectMsg(stadiumNotifyMsg);
    }

    @Override
    public void officialGameEventTeamJoinUpdate(StadiumNotifyMsg stadiumNotifyMsg) {
        stadiumInternalService.officialGameEventTeamJoinUpdate(stadiumNotifyMsg);
    }



}
